home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Dylan alpha demos / Online Insultant Pro src ƒ / Sound.dylan < prev   
Encoding:
Text File  |  1994-12-13  |  3.0 KB  |  89 lines  |  [TEXT/ttxt]

  1. language: infix-dylan
  2. module: Online-Insultant
  3.  
  4. /* Copyright (C) 1994, Apple Computer, Inc. All rights reserved. */
  5.  
  6. // This reads the insult out loud, in case the user is illiterate
  7.  
  8. // Import the Speech Manager interface
  9. // I copied the file from Feb 94 Developer CD to the project directory
  10. //--- Note that this only works with the "universal" headers
  11. define interface
  12.   #include "Speech.h",
  13.      define: {"SystemSevenOrLater"},
  14.      import: {"gestaltSpeechAttr", "gestaltSpeechMgrPresent",
  15.               "SpeakString", "SpeakText", "SpeechBusy",
  16.               "MakeVoiceSpec", "VoiceSpec",
  17.               "NewSpeechChannel", "DisposeSpeechChannel", "SpeechChannel",
  18.               "voiceNotFound",
  19.               // fun stuff for playing around, delete later
  20.               "CountVoices", "GetIndVoice",
  21.               "VoiceDescription", "GetVoiceDescription"};
  22.   function "SpeakText", argument-type: {textBuf => <C-string>};
  23.   function "NewSpeechChannel", output-argument: chan;
  24.   function "CountVoices", output-argument: numVoices;
  25. end interface;
  26.  
  27. // We'll need Gestalt also
  28. define interface
  29.   #include "Gestalt.h",
  30.      // Gestalt was not a trap in System 6
  31.      define: {"SystemSevenOrLater"},
  32.      import: {"Gestalt"};
  33.   function "Gestalt", output-argument: response;
  34. end interface;
  35.  
  36. // We need this so we can free a text-buffer
  37. define interface
  38.   #include "memory.h",
  39.      import: {"DisposePtr"};
  40. end interface;
  41.  
  42. define constant Speech-Available? = method()
  43.   let (err, result) = Gestalt($gestaltSpeechAttr);
  44.   err = 0 & logbit?($gestaltSpeechMgrPresent, result);
  45. end method;
  46.  
  47. define constant await-speech-done = method()
  48.   until ( SpeechBusy() = 0 ) end;
  49. end method;
  50.  
  51. // This compressed high-quality female voice seems to sound best
  52. define constant $preferred-voice :: <VoiceSpec> 
  53.           = begin
  54.               let v = make(<VoiceSpec>);
  55.               MakeVoiceSpec(OSType("gala"), 0, v);
  56.               v;
  57.             end;
  58.  
  59. // returns error code, or if successfully started asynchronous speech, values(chan, text-buffer)
  60. define constant speak-string = method(string :: <string>, #key synchronous)
  61.   let (err, chan) = NewSpeechChannel($preferred-voice);
  62.   let (err, chan) = if ( err = $voiceNotFound )
  63.                       // The preferred voice does not exist, so use the default system voice
  64.                       NewSpeechChannel(as(<VoiceSpec>, 0));
  65.                       else values(err, chan);
  66.                       end if;
  67.   if (err = 0)
  68.     let text-buffer = as(<C-string>, string);   // this makes a copy in the heap
  69.     let err = SpeakText(chan, text-buffer, size(string));
  70.     if (err = 0)
  71.       if (synchronous)
  72.         finish-speak-string(chan, text-buffer);
  73.         err;
  74.       else values(chan, text-buffer);
  75.       end if;
  76.     else err;
  77.     end if;
  78.   else err;
  79.   end if;
  80. end method;
  81.  
  82. // call this with the values from an asynchronous speak-string
  83. define constant finish-speak-string = method(chan, text-buffer)
  84.   if (instance?(chan, <SpeechChannel>))
  85.     await-speech-done();
  86.     DisposeSpeechChannel(chan);
  87.     DisposePtr(as(<machine-pointer>, text-buffer));
  88.   end if;
  89. end method;